var util = require("../../utils/util.js")
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    auctionList: [],
    openid: '',
    num: '1',
    order: '',
    updown:false,
    page:1,
    show: false,
  },
  showxy() {
    console.log('aaa');
    setTimeout(() => {
      this.setData({
        show: true
      });
    }, 50);
  },
  onPullDownRefresh() {

    wx.showNavigationBarLoading(); //在标题栏中显示加载
    var that = this;
    wx.request({
      url: app.serverUrl + 'About/timeMall',
      // header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'GET',
      dataType: 'json',
      data: {
        openid: app.openid,
        page: '1'
      },
      success: function(res) {

        console.log(res);
        for (var i = 0; i < res.data.data.list.length; i++) {
          if (res.data.data.list[i].auction == '0') {
            if (res.data.data.list[i].starttime<=0){
              res.data.data.list[i].time = '';
              res.data.data.list[i].timevalue = '拍卖已结束~'
            }else{
              res.data.data.list[i].time = res.data.data.list[i].starttime;
              res.data.data.list[i].timevalue = '开始时间'
            }

          } else {
            if (res.data.data.list[i].starttime <= 0){
              res.data.data.list[i].time = '';
              res.data.data.list[i].timevalue = '拍卖已结束~'
            }else{
              res.data.data.list[i].time = util.formatTime(res.data.data.list[i].residue, 'h:m');
              res.data.data.list[i].timevalue = '剩余时间'
            }

          }
        }
        that.setData({
          auctionList: res.data.data.list
        })
      },
      complete: function() {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(app.openid);
    wx.showLoading({
      title: '正在加载...',
    })
    var that = this;
    wx.request({
      url: app.serverUrl + 'About/timeMall',
      // header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'GET',
      dataType: 'json',
      data: {
        openid: app.openid,
        page: '1',
      },
      success: function(res) {

        console.log(res);
        for (var i = 0; i < res.data.data.list.length; i++) {
          if (res.data.data.list[i].auction == '0') {
            res.data.data.list[i].time = res.data.data.list[i].starttime;
            res.data.data.list[i].timevalue = '开始时间'
          } else {
            res.data.data.list[i].time = util.formatTime(res.data.data.list[i].residue, 'h:m');
            res.data.data.list[i].timevalue = '剩余时间'
          }
        }
        that.setData({
          auctionList: res.data.data.list
        });
        wx.hideLoading();
      }

    })
  },
  changesort(e) {
    var that = this;
    this.setData({
      num: e.currentTarget.dataset.num
    });
    if (e.currentTarget.dataset.num == '1') {
      this.setData({
        order: '1',
      });
    } else if (e.currentTarget.dataset.num == '2') {
      if (that.data.updown == false){
        this.setData({
          order: '1',
          updown:true,
        })
      }else{
        this.setData({
          order: '2',
          updown: false,
        })
      }

    } else {
      if (that.data.updown == false) {
        this.setData({
          order: '3',
          updown: true,
        })
      } else {
        this.setData({
          order: '4',
          updown: false,
        })
      }
    };
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      url: app.serverUrl + 'About/timeMall',
      // header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'GET',
      dataType: 'json',
      data: {
        openid: app.openid,
        page: '1',
        order: that.data.order
      },
      success: function(res) {

        console.log(res);
        for (var i = 0; i < res.data.data.list.length; i++) {
          if (res.data.data.list[i].auction == '0') {
            res.data.data.list[i].time = res.data.data.list[i].starttime;
            res.data.data.list[i].timevalue = '开始时间'
          } else {
            res.data.data.list[i].time = util.formatTime(res.data.data.list[i].residue, 'h:m');
            res.data.data.list[i].timevalue = '剩余时间'
          }
        }
        that.setData({
          auctionList: res.data.data.list
        });
        wx.hideLoading();
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},
  goaudetail(e) {
    wx.navigateTo({
      url: '/pages/audetail/audetail?goodsid=' + e.currentTarget.dataset.goodsid,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '正在加载...',
    })
    var that = this;
    that.setData({
      page: that.data.page + 1
    })
    wx.request({
      url: app.serverUrl + 'About/timeMall',
      // header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'GET',
      dataType: 'json',
      data: {
        openid: app.openid,
        page: that.data.page
      },
      success: function (res) {
        var arr = res.data.data.list;
        that.setData({
          auctionList: that.data.auctionList.concat(arr)
        });
        wx.hideLoading();
      }

    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})